<template>
  <div class="prodetail">
    <van-nav-bar fixed title="商品详情页" left-arrow @click-left="$router.go(-1)" />

    <van-swipe :autoplay="3000" @change="onChange">
      <van-swipe-item v-for="(image, index) in images" :key="index">
        <img :src="image" />
      </van-swipe-item>

      <template #indicator>
        <div class="custom-indicator">{{ current + 1 }} / {{ images.length }}</div>
      </template>
    </van-swipe>

    <!-- 商品说明 -->
    <div class="info">
      <div class="title">
        <div class="price">
          <span class="now">￥{{list.goods_price_max}}</span>
          <span class="oldprice">￥{{list.line_price_max}}</span>
        </div>
        <div class="sellcount">已售{{list.goods_sales}}件</div>
      </div>
      <div class="msg text-ellipsis-2">{{list.goods_name}}</div>

      <div class="service">
        <div class="left-words">
          <span v-for="item in goodsService" :key="item.service_id"><van-icon name="passed" />{{item.name}}</span>
        </div>
        <div class="right-icon">
          <van-icon name="arrow" />
        </div>
      </div>
    </div>

    <!-- 商品评价 -->
    <div class="comment">
      <div class="comment-title">
        <div class="left">商品评价 ({{limitData.all}}条)</div>
        <div class="right">查看更多 <van-icon name="arrow" /> </div>
      </div>
      <div class="comment-list">
        <div class="comment-item" v-for="(item, index) in ListRows.slice(0,3)" :key="index">
          <div class="top">
            <img :src="item.user.avatar_url || defaultImg" alt="">
            <div class="name">{{item.user.nick_name}}</div>
            <van-rate :size="16" :value="item.score" color="#ffd21e" void-icon="star" void-color="#eee"/>
          </div>
          <div class="content">
            {{item.content}}
          </div>
          <div class="time">
            {{item.create_time}}
          </div>
        </div>
      </div>
    </div>

    <!-- 商品描述 -->
    <div class="desc" v-html="list.content">

    </div>

    <!-- 底部 -->
    <div class="footer">
      <div class="icon-home" @click="$router.push('/')">
        <van-icon name="home-o" />
        <span>首页</span>
      </div>
      <van-badge v-if="cartTotal > 0  " :content=cartTotal max="99">
      <div class="icon-cart">

        <van-icon name="cart-o" />
        <span @click="$router.push('/cart')">购物车</span>
      </div>
    </van-badge>
    <div v-else class="icon-cart">

<van-icon name="cart-o" />
<span @click="$router.push('/cart')">购物车</span>
</div>
      <div @click="addFn" class="btn-add">加入购物车</div>
      <div @click="buyFn" class="btn-buy">立刻购买</div>

      <!-- 弹出动画 -->
      <van-action-sheet  v-model="showPannel" :title="mode === 'cart' ? '加入购物车' : '立刻购买'">
        <div class="product">
    <div class="product-title">
      <div class="left">
        <img :src=list.goods_image alt="">
      </div>
      <div class="right">
        <div class="price">
          <span>¥</span>
          <span class="nowprice">{{list.goods_price_max}}</span>
        </div>
        <div class="count">
          <span>库存</span>
          <span>{{list.stock_total}}</span>
        </div>
      </div>
    </div>
    <div class="num-box">
      <span>数量</span>
      <CountBox v-model="addCount"></CountBox>
    </div>
    <div class="showbtn" v-if="list.stock_total > 0">
      <div @click="addCart" class="btn" v-if="mode === 'cart'">加入购物车</div>
      <div @click="goBuyNow" class="btn now" v-else>立刻购买</div>
    </div>
    <div class="btn-none" v-else>该商品已抢完</div>
  </div>
</van-action-sheet>

  <!-- 提示窗 -->
  <!-- <van-dialog v-model="show" title="标题" show-cancel-button>
  <img src="https://img01.yzcdn.cn/vant/apple-3.jpg" />
</van-dialog> -->
    </div>
  </div>
</template>

<script>
import { getProDetail, getGoodsService, getProDetailtotal, getProDetailListRows } from '@/api/prodetail'
import { cartAdd, getCartTotal } from '@/api/cart'
import defaultImg from '@/assets/default-avatar.png'
import CountBox from '@/components/CountBox.vue'
import loginConfirm from '@/mixins/loginConfirm'

export default {
  name: 'ProdetailIndex',
  mixins: [loginConfirm],
  components: {
    CountBox
  },
  data () {
    return {
      addCount: 1,
      mode: 'cart',
      showPannel: false,
      list: {}, // 商品详情数据
      goodsService: [], // 商品保障服务
      limitData: {}, // 评价数量
      ListRows: [], // 评价
      images: [], // 轮播图
      current: 0,
      scoreType: '-1', // -1 全部 10 好评 20 中评 30 差评
      defaultImg, // 默认头像
      cartTotal: 0 // 购物车商品总数
    }
  },
  computed: {
    // 获取地址栏的关键字
    queryGoodsid () {
      return this.$route.query.goodsid
    }
    // 获取购物车总数
    // ...mapGetters('cart', ['cartTotal'])

  },
  async created () {
    // 获取商品详情数据
    const res = await getProDetail(this.queryGoodsid
    )
    // console.log(res.data.detail)
    this.list = res.data.detail
    this.images = this.list.goods_images.map(item => {
      return item.external_url
    })

    // 获取商品保障服务
    const goodsService = await getGoodsService(this.queryGoodsid)
    // console.log(goodsService.data.list)
    this.goodsService = goodsService.data.list

    // 获取商品评论总数
    const limit = await getProDetailtotal(this.queryGoodsid)
    this.limitData = limit.data.total
    // console.log(limit.data.total)

    // 获取商品评论
    const ListRows = await getProDetailListRows(limit.data.total.all, this.queryGoodsid)
    // console.log(ListRows.data.list)
    this.ListRows = ListRows.data.list

    // 获取购物车总数
    this.getcartTotal()
  },
  methods: {
    // 获取购物车总数
    async getcartTotal () {
      const { data } = await getCartTotal()
      // console.log(data.cartTotal)
      this.cartTotal = data.cartTotal
    },
    onChange (index) {
      this.current = index
    },
    onSelect (item) {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      this.showPannel = false
      this.$toast(item.name)
    },
    addFn () {
      this.mode = 'cart'
      this.showPannel = true
    },
    buyFn () {
      this.mode = 'buyNow'
      this.showPannel = true
    },

    async addCart () {
      if (this.loginConfirm()) {
        return
      }
      await cartAdd(this.queryGoodsid, this.addCount, this.list.skuList[0].goods_sku_id)

      let search = 3
      let toast1
      setInterval(() => {
        if (search === 3) {
          this.$toast.clear(true)
          this.$toast.allowMultiple()
          toast1 = this.$toast({
            message: '已加入购物车',
            icon: 'passed',
            duration: 0
          })
        }
        search--
        if (!search) {
          toast1.clear()
        }
      }, 1000)
      this.showPannel = false
      this.getcartTotal()
    },
    goBuyNow () {
      if (this.mode === 'buyNow') {
        if (this.loginConfirm()) {
          return
        }
        // 购买页面
        this.$router.push({
          path: '/pay',
          query: {
            mode: 'buyNow',
            goodsId: this.queryGoodsid,
            goodsSkuId: this.list.skuList[0].goods_sku_id,
            goodsNum: this.addCount
          }
        })
      }
      // this.$toast({
      //   message: '购买页面',
      //   icon: 'passed'
      // })
    }
  }
}
</script>

<style lang="less" scoped>

.prodetail {
  padding-top: 46px;
  ::v-deep .van-icon-arrow-left {
    color: #333;
  }
  img {
    display: block;
    width: 100%;
  }
  .custom-indicator {
    position: absolute;
    right: 10px;
    bottom: 10px;
    padding: 5px 10px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 15px;
  }
  .desc {
    width: 100%;
    overflow: scroll;
    ::v-deep img {
      display: block;
      width: 100%!important;
    }
  }
  .info {
    padding: 10px;
  }
  .title {
    display: flex;
    justify-content: space-between;
    .now {
      color: #fa2209;
      font-size: 20px;
    }
    .oldprice {
      color: #959595;
      font-size: 16px;
      text-decoration: line-through;
      margin-left: 5px;
    }
    .sellcount {
      color: #959595;
      font-size: 16px;
      position: relative;
      top: 4px;
    }
  }
  .msg {
    font-size: 16px;
    line-height: 24px;
    margin-top: 5px;
  }
  .service {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    margin-top: 10px;
    font-size: 16px;
    background-color: #fafafa;
    .left-words {
      span {
        margin-right: 10px;
      }
      .van-icon {
        margin-right: 4px;
        color: #fa2209;
      }
    }
  }

  .comment {
    padding: 10px;
  }
  .comment-title {
    display: flex;
    justify-content: space-between;
    .right {
      color: #959595;
    }
  }

  .comment-item {
    font-size: 16px;
    line-height: 30px;
    .top {
      height: 30px;
      display: flex;
      align-items: center;
      margin-top: 20px;
      img {
        width: 20px;
        height: 20px;
      }
      .name {
        margin: 0 10px;
      }
    }
    .time {
      color: #999;
    }
  }

  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 55px;
    background-color: #fff;
    border-top: 1px solid #ccc;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    .icon-home, .icon-cart {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      .van-icon {
        font-size: 24px;
      }
    }
    .btn-add,
    .btn-buy {
      height: 36px;
      line-height: 36px;
      width: 120px;
      border-radius: 18px;
      background-color: #ffa900;
      text-align: center;
      color: #fff;
      font-size: 14px;
    }
    .btn-buy {
      background-color: #fe5630;
    }
  }
}

.tips {
  padding: 10px;
}

/*弹出动画css*/
.product {
  .product-title {
    display: flex;
    .left {
      img {
        width: 90px;
        height: 90px;
      }
      margin: 10px;
    }
    .right {
      flex: 1;
      padding: 10px;
      .price {
        font-size: 14px;
        color: #fe560a;
        .nowprice {
          font-size: 24px;
          margin: 0 5px;
        }
      }
    }
  }

  .num-box {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    align-items: center;
  }

  .btn, .btn-none {
    height: 40px;
    line-height: 40px;
    margin: 20px;
    border-radius: 20px;
    text-align: center;
    color: rgb(255, 255, 255);
    background-color: rgb(255, 148, 2);
  }
  .btn.now {
    background-color: #fe5630;
  }
  .btn-none {
    background-color: #cccccc;
  }
}
</style>
